﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.XetTotNghiep
{
    public partial class UcQuanLyDanhSachDeNghiXetTotNghiep : QldtSdh.GUI.Common.BaseUserControl
    {
        bool allowUpdate = false;
        FormInBangDiemFinal formInBangDiemFinal = null;
        void InitGridHocVien()
        {
            gridHocVien.AutoGenerateColumns = false;
            colMaHocVien.DataPropertyName = "MaHocVien";
            colHoDem.DataPropertyName = "HoDem";
            colTen.DataPropertyName = "Ten";
            colGioiTinh.DataPropertyName = "_GioiTinhStr";
            colNgaySinh.DataPropertyName = "NgaySinh";
            colChungChi.DataPropertyName = "ChungChi";
            colDiemTrungBinhChungTichLuy.DataPropertyName = "_DiemTichLuy";
            colTongSoTinChiTichLuy.DataPropertyName = "_TinChiTichLuy";
            colDiemLuanVan.DataPropertyName = "_DiemLuanVan";
            colSelected.DataPropertyName = "_IsSelected";
        }

        public UcQuanLyDanhSachDeNghiXetTotNghiep()
        {
            InitializeComponent();
            InitGridHocVien();
            LoadKhoaHoc();
        }

        void LoadKhoaHoc()
        {
            allowUpdate = false;
            cbxKhoaHoc.DataSource = BizKhoaHoc.GetList();
            cbxKhoaHoc.DisplayMember = "TenKhoaHoc";
            cbxKhoaHoc.ValueMember = "MaKhoaHocGUID";
            LoadNganhHoc();
        }

        void LoadNganhHoc()
        {
            var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
            if (khoaHoc != null)
            {
                var ls = BizKhoaHoc_NganhDaoTao.GetListByKhoaHoc2(khoaHoc.MaKhoaHocGUID);
                cbxNganhHoc.DataSource = ls;
                cbxNganhHoc.DisplayMember = "TenNganhDaoTao";
                cbxNganhHoc.ValueMember = "MaNganhDaoTaoGUID";
            }
            else
                cbxKhoaHoc.Items.Clear();
            LoadHocVien();
        }

        void LoadHocVien()
        {
            var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
            var nganh = cbxNganhHoc.SelectedItem as NganhDaoTao;
            btnChonTatCa.Enabled = false;
            btnPreview.Enabled = btnInBangDiemSelected.Enabled = false;
            bdHocVien.DataSource = null;
            if (khoaHoc != null && nganh != null)
            {
                var ls = BizHocVien.GetListTheoKhoa(khoaHoc.MaKhoaHocGUID, nganh.MaNganhDaoTaoGUID, true);
                bdHocVien.DataSource = ls;
                if (ls != null && ls.Count > 0)
                    btnPreview.Enabled = btnChonTatCa.Enabled = btnInBangDiemSelected.Enabled = true;
            }
            gridHocVien.DataSource = bdHocVien;
            allowUpdate = true;
        }

        private void cbxKhoaHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadNganhHoc();
        }

        private void cbxNganhHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
                LoadHocVien();
        }

        bool selected = true;
        private void btnChonTatCa_Click(object sender, EventArgs e)
        {
            var ls = bdHocVien.DataSource as List<HocVien>;
            if (ls != null && ls.Count > 0)
            {
                foreach (var hocVien in ls)
                    hocVien._IsSelected = selected;
                selected = !selected;
                gridHocVien.Refresh();
            }
        }

        private void btnInBangDiemSelected_Click(object sender, EventArgs e)
        {
            gridHocVien.EndEdit();
            var ls = bdHocVien.DataSource as List<HocVien>;
            if (ls != null && ls.Count > 0)
            {
                int i = 0;
                foreach (var hocVien in ls)
                {
                    i++;
                    if (hocVien._IsSelected)
                    {
                        bool duKD = false;
                        var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
                        var lop = BizLopKhoaNganh.GetByID(hocVien.MaLopKhoaNganhGUID);
                        var nganh = cbxNganhHoc.SelectedItem as NganhDaoTao;
                        if (khoaHoc != null && lop != null && nganh != null)
                        {
                            var ctdt = BizChuongTrinhDaoTao.LoadCTDT(hocVien, lop, out duKD);
                            if (ctdt != null && ctdt.Count > 0)
                            {
                                if (formInBangDiemFinal == null)
                                    formInBangDiemFinal = new FormInBangDiemFinal();
                                formInBangDiemFinal.ShowOrPrint(hocVien, ctdt, nganh, khoaHoc.NamTuyenSinh, false);
                            }
                        }
                    }
                }
            }
        }

        private void btnXetLai_Click(object sender, EventArgs e)
        {
            var ls = bdHocVien.DataSource as List<HocVien>;
            if (ls != null && ls.Count > 0)
            {
                int i = 0;
                int total = ls.Count;
                int countSuccess = 0;
                foreach (var hocVien in ls)
                {
                    i++;
                    if (hocVien._IsSelected)
                    {
                        hocVien.DaTotNghiep = false;
                        var rs = BizHocVien.Update(hocVien);
                        if (rs == CODE_RESULT_RETURN.ThanhCong)
                            countSuccess++;
                    }
                }
                DialogUtils.ShowMessage(string.Format("Đã loại {0} học viên ra khỏi danh sách tốt nghiệp", countSuccess));
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            var hocVien = bdHocVien.Current as HocVien;
            bool duKD = false;
            var khoaHoc = cbxKhoaHoc.SelectedItem as KhoaHoc;
            var lop = BizLopKhoaNganh.GetByID(hocVien.MaLopKhoaNganhGUID);
            var nganh = cbxNganhHoc.SelectedItem as NganhDaoTao;
            if (khoaHoc != null && lop != null && nganh != null)
            {
                var ctdt = BizChuongTrinhDaoTao.LoadCTDT(hocVien, lop, out duKD);
                if (ctdt != null && ctdt.Count > 0)
                {
                    if (formInBangDiemFinal == null)
                        formInBangDiemFinal = new FormInBangDiemFinal();
                    formInBangDiemFinal.ShowOrPrint(hocVien, ctdt, nganh, khoaHoc.NamTuyenSinh, true);
                }
            }
        }
    }
}
